﻿using UnityEngine;
using System.Collections;
using WoogiUI;
using System.Collections.Generic;

[ExecuteInEditMode()]
public class UICamera : MonoBehaviour
{
    private static UICamera instance;
    public static UICamera Instance
    {
        get
        {
            if (instance == null)
            {
                instance = GameObject.FindObjectOfType<UICamera>();
                if (instance == null)
                {
                    Debug.LogError(" dont font UICamera!");
                }

            }
            return instance;
        }
    }

    public void Awake()
    {
        UiCamera.orthographic = true;
    }

    public Camera UiCamera
    {
        get
        {
            return GetComponent<Camera>();
        }
    }
    [SerializeField]
    private bool isOpenUICamera = false;

    public bool IsOpenUICamera
    {
        get
        {
            if (gameObject.activeSelf != isOpenUICamera)
            {
                gameObject.SetActive(isOpenUICamera);
            }
            return isOpenUICamera;
        }
    }

    public bool UICameraAble
    {
        set
        {
            UiCamera.enabled = value;
        }
    }

    Vector2 screenSize = new Vector2(1920f, 1080f);
    void Update()
    {
        if (Screen.width != screenSize.x || Screen.height != screenSize.y)
        {
            screenSize = new Vector2(Screen.width, Screen.height);
            float size = UIEffectSize();
            UiCamera.orthographicSize = (2 - size) * 5f;
        }
    }

    private float UIEffectSize()
    {
        float currentSize = (Screen.width / (float)Screen.height);
        if (currentSize != 16 / (float)9)
        {
            currentSize = currentSize / (16 / (float)9);
        }
        else
            currentSize = 1f;
        return currentSize;
    }
}
